<?php
/**
 * Created by PhpStorm.
 * User: 魏国荣
 * Date: 2019/2/23
 * Time: 8:58
 */

namespace App\Http\Controllers\Register;

use App\Http\Controllers\BaseApiController;
use App\Services\Register\RegisterService;
use App\Services\Company\CompanyService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Validator;
class RegisterController extends BaseApiController
{
    private $service;
    private $company;
    public function __construct(RegisterService $service)
    {
        //继承基类的构造方法
        parent::__construct();
        //依赖注入service
        $this->service=$service;
        $com = new CompanyService();
        $this->company = $com;
    }

    /*验证码发送接口*/
    public function getCode(Request $request)
    {
        $phone = $request->input('coachid');//获取手机号码\
        //$phone = 18501692506;
        $tempid = '51463';
        $appkey = '34925';
        $sign = 'c16bfba342b7decc008cdc3eafc6d04a';
        $code = rand(1000,9999);
        Redis::set("$phone",$code);
        $code1 = 'code='.$code;
        $param = urlencode($code1);
        $this->curl($tempid,$appkey,$phone,$sign,$param);
        return json_encode($code);
    }

    /*curl发送信息接口*/
    public function curl($tempid,$appkey,$phone,$sign,$param)
    {
        $url="http://api.k780.com/?app=sms.send&tempid=$tempid&param=$param&phone=$phone&appkey=$appkey&sign=$sign";
        file_get_contents($url);
        /*$ch = curl_init();
        // 设置URL和相应的选项
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, false);
        // 抓取URL并把它传递给浏览器
        curl_exec($ch);
        //关闭cURL资源，并且释放系统资源
        curl_close($ch);*/
    }

   /*
    * 验证手机号是否被注册
    */
    public function isHave(Request $request)
    {
        $phone = $request->input('coachid');//获取手机号码

        $res = $this->service->isPhone($phone);

        if($res)
        {
            return 1;//返回已注册标注
        }
    }
    //验证数据
    public function isYes(Request $request)
    {
        //验证规则
        $validator = Validator::make($request->all(),[
            'phone'=>'required|unique:usera|max:12',
            'password'=>'required|max:18|min:6|same:rpassword',
            'rpassword'=>'required|max:18|min:6|same:password',
        ],[
            'phone.required'=>'手机号不能为空',
            'phone.unique:usera'=>'手机号已注册',
            'password.required'=>'密码最大为18位，最少为6位',
            'rpassword.required'=>'密码最大为18位，最少为6位',
            'password.same:rpassword'=>'密码与确认密码不一致',
            'rpassword.same:password'=>'两次密码不一致',
        ]);

        if($validator->fails()){
            $failed=$validator->errors();
            return $failed;
        }
        //信息输入正确添加入库
        date_default_timezone_set('PRC');
        $phone = $request->input('phone');
        $number = $request->input('code');
        $code = Redis::get("$phone");
        if($code!=$number)
        {
            return ['code'=>'400','msg'=>'验证码错误'];
        }
        $password = $request->input('password');
        $time = date('Y-m-d H:i:s',time());
        $result = $this->service->addUser($phone,$password,$time);

        if($result)
        {
            //注册的同时添加默认公司信息
            $image = 'http://static.lagou.com/thumbnail_300x300/i/image/M00/3C/A7/CgpFT1lTIVeAChyRAABczi8mykw730.jpg';
            $name = '某某有限公司';
            $is_hot = 1;
            $tag= '互联网';
            $credit_code= '500';
            $address= '上海市';

            $created_at = date('Y-m-d H:i:s',time());
            $updated_at = date('Y-m-d H:i:s',time());
            $num = 1000;

            $data = $this->company->addCompany($image,$name,$is_hot,$tag,$credit_code,$address,$created_at,$updated_at,$num);
            if($data) {
                return ['code' => '200', 'msg' => 'success'];
            }
        }else{
            return ['code'=>'304','msg'=>'error'];
        }
    }

    /*登录接口*/
    public function loginUser(Request $request)
    {
        $data= $request->all();
        $rules=[
            'phone'=>'required',
            'password'=>'required',
        ];
        $error=[
            'phone.required'=>'手机号不能为空',
            'password.required'=>'密码不能为空',
        ];
        $validator=$this->validator($data,$rules,$error);
        if($validator->fails()){
            $failed=$validator->errors();
            return $failed;
        }
        //接收数据
        $phone = $request->input('phone');
        $pwd = $request->input('password');
        $res = $this->service->haveUser($phone,$pwd);
        //print_r($res);die;
        //print_r($id);die;
        //print_r($type);die;
        if($res)
        {
            //生成token
            $token = md5('register'.'six').'_'.$res['id'];
            Redis::set("$token",json_encode($res));
            //print_r($token);die;
            $type = $res['tag_id'];
            return ['code'=>'200','msg'=>'success','type'=>$type,'token'=>$token];
        }else{
            return ['code'=>'304','msg'=>'error'];
        }
    }

    /*忘记密码接口*/
    public function forgetPwd(Request $request)
    {
        //验证规则--->封装调用
        $data= $request->all();
        $rules=[
            'phone'=>'required|max:12',
            'password'=>'required|max:18|min:6|same:rpassword',
            'rpassword'=>'required|max:18|min:6|same:password',
        ];
        $error=[
            'phone.required'=>'手机号不能为空',
            'password.required'=>'密码最大为18位，最少为6位',
            'rpassword.required'=>'密码最大为18位，最少为6位',
            'password.same:rpassword'=>'密码与确认密码不一致',
            'rpassword.same:password'=>'两次密码不一致',
        ];
        $validator=$this->validator($data,$rules,$error);
        if($validator->fails()){
            $failed=$validator->errors();
            return $failed;
        }
        //信息修改验证正确修改数据库
        date_default_timezone_set('PRC');
        $phone = $request->input('phone');
        //查询用户ID
        $data = $this->service->isPhone($phone);
        $id  = $data[0]['id'];
        $number = $request->input('code');
        $code = Redis::get("$phone");
        if($code!=$number)
        {
            return ['code'=>'400','msg'=>'验证码错误'];
        }
        $password = $request->input('password');
        $result = $this->service->forgetInfo($password,$id);
        if($result)
        {
            return ['code'=>'200','msg'=>'success'];
        }else{
            return ['code'=>'304','msg'=>'error'];
        }
    }
}